package com.idrive.activity;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.idrive.R;

public class VoiceMessageActivity extends Activity {

	private static String fileName;
	private MediaRecorder mediaRecorder;
	private MediaPlayer mediaPlayer;

	public VoiceMessageActivity() {
		fileName ="/sdcard/voicemessage.3gp";

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_voice);

		final ToggleButton recordToggleButton = (ToggleButton) findViewById(R.id.recordToggleButton);

		recordToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {

				if (isChecked) {
					startRecording();
					Toast.makeText(VoiceMessageActivity.this, "Message recorder has been started.", Toast.LENGTH_LONG).show();
				} else {
					stopRecording();
					startPlaying();
					stopPlaying();
					Toast.makeText(VoiceMessageActivity.this,
					        "Recorded voice message will be automatically played on an incoming call.",
					        Toast.LENGTH_LONG).show();

					Intent myIntent = new Intent(VoiceMessageActivity.this, CallManagerActivity.class);

					VoiceMessageActivity.this.startActivity(myIntent);
				}
			}
		});
	}

	private void startPlaying() {
		mediaPlayer = new MediaPlayer();

		try {
			mediaPlayer.setDataSource(fileName);
			mediaPlayer.prepare();
			mediaPlayer.start();
		} catch (IOException e) {

			e.printStackTrace();
		}

	}

	private void stopPlaying() {
		mediaPlayer.release();
		mediaPlayer = null;
	}

	private void startRecording1() {
		mediaRecorder = new MediaRecorder();
		mediaRecorder.reset();
		mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		mediaRecorder.setOutputFile(fileName);
		mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

		try {
			mediaRecorder.prepare();
		} catch (IOException e) {
			e.printStackTrace();
		}

		mediaRecorder.start();
	}

	private void stopRecording1() {
		mediaRecorder.stop();
		mediaRecorder.release();
		mediaRecorder = null;
	}

	boolean recording = false;

	private void startRecording() {
		
		mediaRecorder = new MediaRecorder();
		
		settings();
		recordPrepare();
		mediaRecorder.start();
		recording = true;
	}

	private void stopRecording() {
		if (recording) {
			mediaRecorder.stop();
			mediaRecorder.release();
			recording = false;
			settings();
			recordPrepare();
		}
	}

	// Settings for Audio Recorder
	private void settings() {
		mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
		mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
		mediaRecorder.setOutputFile(fileName);
	}

	// prepare the Environment for Recording
	private void recordPrepare() {
		try {
			mediaRecorder.prepare();
		} catch (IllegalStateException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_voice, menu);
		return true;
	}

}
